﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using GC2.Entities.Seguridad;
using GC2.Entities.Admin;
using GC2.Bussines.BMAdmin;

public partial class Admin_CierreCaja : GenericPage
{
    #region Propiedades
    Usuarios usuario = new Usuarios();
    private Decimal dEfectivo
    {
        get
        {
            return (Decimal)Session["dEfectivo"];
        }
        set
        {
            Session["dEfectivo"] = value;
        }
    }

    private Decimal dValores
    {
        get
        {
            return (Decimal)Session["dValores"];
        }
        set
        {
            Session["dValores"] = value;
        }
    }

    private Decimal dTotal
    {
        get
        {
            return (Decimal)Session["dTotal"];
        }
        set
        {
            Session["dTotal"] = value;
        }
    }

    Cajas caja = new Cajas();
    int iCantidad = 0;
    Decimal dMonto = 0;
    decimal dDif = 0;
    Decimal dADif = 0;
    GC2.Funciones.General.FunctionsSite f = new GC2.Funciones.General.FunctionsSite();
    

    #endregion

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
                dEfectivo = 0;
                dValores = 0;
                dTotal = 0;
                lblAccion.Text = "Carga de Dinero de Apertura y/o Cierre.";
                IniciaCaja();
                trCajas1.Visible = false;
                trCajas2.Visible = false;
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region Monedas

    protected void txtACtv5_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtACtv5.Text == "")
            {
                txtACtv5.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtACtv10_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtACtv10.Text == "")
            {
                txtACtv10.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtACtv25_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtACtv25.Text == "")
            {
                txtACtv25.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtACtv50_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtACtv50.Text == "")
            {
                txtACtv50.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtACtv100.Text == "")
            {
                txtACtv100.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtCtv5_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtCtv5.Text == "")
            {
                txtCtv5.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtCtv10_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtCtv10.Text == "")
            {
                txtCtv10.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtCtv25_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtCtv25.Text == "")
            {
                txtCtv25.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtCtv50_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtCtv50.Text == "")
            {
                txtCtv50.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtCtv100.Text == "")
            {
                txtCtv100.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Billetes

    protected void txtA2_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA2.Text == "")
            {
                txtA2.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA5_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA5.Text == "")
            {
                txtA5.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA10_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA10.Text == "")
            {
                txtA10.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA20_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA20.Text == "")
            {
                txtA20.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA50_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA50.Text == "")
            {
                txtA50.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txtA100_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtA100.Text == "")
            {
                txtA100.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt2_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt2.Text == "")
            {
                txt2.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt5_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt5.Text == "")
            {
                txt5.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt10_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt10.Text == "")
            {
                txt10.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt20_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt20.Text == "")
            {
                txt20.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt50_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt50.Text == "")
            {
                txt50.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void txt100_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txt100.Text == "")
            {
                txt100.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Cheques
    protected void txtATCheques_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtATCheques.Text == "")
            {
                txtATCheques.Text = "0";
            }
            RecalculaA();
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    protected void txtTCheques_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (txtTCheques.Text == "")
            {
                txtTCheques.Text = "0";
            }
            Recalcula();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnGuardar_Click
    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        try
        {
            //Param param = new Param();
            //BMParam bmParam = new BMParam();
            //param.Id_Param = 1;
            //param = bmParam.BMParamGetOne(param);
            //bmParam.BMParamUpdate(param);

            // Grabar Caja Apertura y Cierre
            Cajas caja = new Cajas();
            BMCajas bmCajas = new BMCajas(Connection);

            caja = DeshidrataA();
            caja.Id_Caja = Convert.ToInt64(lblCaja.Text);
            caja.Tipo = 1;
            caja.Estado = 1;
            bmCajas.BMCajasUpdate(caja);

            caja = DeshidrataC();
            caja.Id_Caja = Convert.ToInt64(lblCaja.Text);
            caja.Tipo = 2;
            caja.Estado = 1;
            bmCajas.BMCajasUpdate(caja);
            lblAccion.Text = "Caja Guardada";

            //param = null;
            //bmParam = null;
            caja = null;
            bmCajas = null;
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnCerrar_Click
    protected void btnCerrar_Click(object sender, EventArgs e)
    {
        try
        {
            CerrarCaja();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Seguridad/Default.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion    

    #region gvCaja_PageIndexChanging
    /// <Summary>
    /// CBendayan 29/05/2008
    /// </Summary>
    /// <returns></returns>
    protected void gvCaja_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvCajas.PageIndex = e.NewPageIndex;
            ActualizaGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region  btnPasar_Click
    protected void btnPasar_Click(object sender, EventArgs e)
    {
        try
        {
            if (f.isNumeric(txtCaja.Text))
            {
                Int64 iCaja = Convert.ToInt64(txtCaja.Text);
                if (iCaja > 0)
                {
                    List<Cajas> listaCajas = (List<Cajas>)Session["listaCajas"];
                    for (int i = 0; i < listaCajas.Count; i++)
                    {
                        if (listaCajas[i].Id_Caja == iCaja)
                        {
                            Session["CajaDestino"] = listaCajas[i];
                            PasarCaja();
                            btnPasar.Enabled = false;
                            lblAccion.Text = "Total de valores pasados a Caja:" + iCaja.ToString();
                            return;
                        }
                    }
                    lblAccion.Text = "Caja No disponible para pasar valores.";
                }
                else
                {
                    lblAccion.Text = "Caja cerrada sin pasar valores.";
                    btnPasar.Enabled = false;
                }
            }
            else
            {
                lblAccion.Text = "Error en Numero de Caja";
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

    #region Métodos

    #region IniciaCaja
    protected void IniciaCaja()
    {
        try
        {
            caja = (Cajas)Session["Caja"];
            lblCaja.Text = caja.Id_Caja.ToString();
            lblFecha.Text = caja.Fecha.ToShortDateString();
            decimal decTemp = 0;
            //decTemp = Convert.ToDecimal(caja.CtaCte);
            //decTemp = Math.Round(decTemp, 2);
            //dCtaCte = caja.CtaCte;
            dValores = caja.Total; // -caja.CtaCte;
            decTemp = Convert.ToDecimal(dValores);
            decTemp = Math.Round(decTemp, 2);
            dValores = 0;
            txtTValores.Text = "0,00";
            txtATValores.Text = "0,00";
            decTemp = Convert.ToDecimal(caja.Total);
            decTemp = Math.Round(decTemp, 2);
            dTotal = caja.Total;
            txtTGeneral.Text = decTemp.ToString();
            dDif = Convert.ToDecimal(dValores - dTotal);
            //dTotal = caja.Total - caja.CtaCte;
            decTemp = Convert.ToDecimal(dDif);
            decTemp = Math.Round(decTemp, 2);
            txtTDifer.Text = decTemp.ToString();

            caja = new Cajas();
            caja = (Cajas)Session["Caja"];
            caja.Tipo = 1;
            BMCajas bmCajas = new BMCajas(Connection);
            caja = bmCajas.BMCajasGetOne(caja);
            decimal dTemp = 0;
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv5), 2);
            txtACtv5.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv10), 2);
            txtACtv10.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv25), 2);
            txtACtv25.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv50), 2);
            txtACtv50.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv100), 2);
            txtACtv100.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete2), 2);
            txtA2.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete5), 2);
            txtA5.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete10), 2);
            txtA10.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete20), 2);
            txtA20.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete50), 2);
            txtA50.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete100), 2);
            txtA100.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Cheques), 2);
            txtATCheques.Text = dTemp.ToString();

            RecalculaA();

            caja = new Cajas();
            caja = (Cajas)Session["Caja"];
            caja.Tipo = 2;
            caja = bmCajas.BMCajasGetOne(caja);
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv5), 2);
            txtCtv5.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv10), 2);
            txtCtv10.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv25), 2);
            txtCtv25.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv50), 2);
            txtCtv50.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Ctv100), 2);
            txtCtv100.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete2), 2);
            txt2.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete5), 2);
            txt5.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete10), 2);
            txt10.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete20), 2);
            txt20.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete50), 2);
            txt50.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Billete100), 2);
            txt100.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(caja.Cheques), 2);
            txtTCheques.Text = dTemp.ToString();
            txtObs.Text = caja.Obs;

            Recalcula();

            dDif = Convert.ToDecimal(dValores - dTotal - dADif);
            
            decTemp = Convert.ToDecimal(dDif);
            decTemp = Math.Round(decTemp, 2);
            txtTDifer.Text = decTemp.ToString();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region RecalculaA
    protected void RecalculaA()
    {
        try
        {
            dValores = 0;
            dEfectivo = 0;
            dADif = 0;

            if (txtACtv5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtACtv5.Text.Trim());
                dMonto = Convert.ToDecimal(0.05 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtACtv10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtACtv10.Text.Trim());
                dMonto = Convert.ToDecimal(0.10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtACtv25.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtACtv25.Text.Trim());
                dMonto = Convert.ToDecimal(0.25 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtACtv50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtACtv50.Text.Trim());
                dMonto = Convert.ToDecimal(0.50 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtACtv100.Text != "0")
            {
                dMonto = Convert.ToInt16(txtACtv100.Text.Trim());
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA2.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA2.Text.Trim());
                dMonto = Convert.ToDecimal(2 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA5.Text.Trim());
                dMonto = Convert.ToDecimal(5 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA10.Text.Trim());
                dMonto = Convert.ToDecimal(10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA20.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA20.Text.Trim());
                dMonto = 20 * iCantidad;
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA50.Text.Trim());
                dMonto = 50 * iCantidad;
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtA100.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtA100.Text.Trim());
                dMonto = 100 * iCantidad;
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtATCheques.Text != "0")
            {
                dMonto = Convert.ToDecimal(txtATCheques.Text.Trim());
                dValores += dMonto;
            }

            decimal decTemp = Convert.ToDecimal(dValores);
            decTemp = Math.Round(decTemp, 2);
            txtATValores.Text = decTemp.ToString();
            decTemp = Convert.ToDecimal(dEfectivo);
            decTemp = Math.Round(decTemp, 2);
            dADif = dValores;
            Session["dADif"] = dADif;
            txtATEfectivo.Text = decTemp.ToString();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region Recalcula
    protected void Recalcula()
    {
        try
        {
            dValores = 0;
            dEfectivo = 0;

            if (txtCtv5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv5.Text.Trim());
                dMonto = Convert.ToDecimal(0.05 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv10.Text.Trim());
                dMonto = Convert.ToDecimal(0.10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv25.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv25.Text.Trim());
                dMonto = Convert.ToDecimal(0.25 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv50.Text.Trim());
                dMonto = Convert.ToDecimal(0.50 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv100.Text != "0")
            {
                dMonto = Convert.ToInt16(txtCtv100.Text.Trim());
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt2.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt2.Text.Trim());
                dMonto = Convert.ToDecimal(2 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt5.Text.Trim());
                dMonto = Convert.ToDecimal(5 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt10.Text.Trim());
                dMonto = Convert.ToDecimal(10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt20.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt20.Text.Trim());
                dMonto = Convert.ToDecimal(20 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt50.Text.Trim());
                dMonto = Convert.ToDecimal(50 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt100.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt100.Text.Trim());
                dMonto = Convert.ToDecimal(100 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtTCheques.Text != "0")
            {
                dMonto = Convert.ToDecimal(txtTCheques.Text.Trim());
                dValores += dMonto;
            }

            decimal decTemp = Convert.ToDecimal(dValores);
            decTemp = Math.Round(decTemp, 2);
            txtTValores.Text = decTemp.ToString();
            decTemp = Convert.ToDecimal(dEfectivo);
            decTemp = Math.Round(decTemp, 2);
            txtTEfectivo.Text = decTemp.ToString();
            dADif = (Decimal)Session["dADif"];
            dDif = Convert.ToDecimal(dValores - dTotal - dADif);
            dDif = Math.Round(dDif, 2);
            txtTDifer.Text = dDif.ToString();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region CerrarCaja
    protected void CerrarCaja()
    {
        try
        {
            // Grabar Caja Apertura y Cierre
            Cajas caja = new Cajas();
            BMCajas bmCajas = new BMCajas(Connection);

            caja = DeshidrataA();
            caja.Tipo = 1;
            caja.Estado = 0;
            bmCajas.BMCajasUpdate(caja);

            caja = DeshidrataC();
            caja.Tipo = 2;
            caja.Estado = 0;
            bmCajas.BMCajasUpdate(caja);

            Session["Caja"] = caja;

            trCajas1.Visible = true;
            trCajas2.Visible = true;

            Cajas cajas = new Cajas();
            cajas.Tipo = 2;
            cajas.Estado = 2;

            List<Cajas> listaCajas = new List<Cajas>();

            listaCajas = bmCajas.BMCajasSearch(cajas, cajas);

            

            Session["listaCajas"] = listaCajas;

            trCajas1.Visible = true;
            trCajas2.Visible = true;

            ActualizaGrilla();
             
            //// Generar proxima caja

            //Param param = new Param();
            //BMParam bmParam = new BMParam();
            //param.Id_Param = 1;
            //param = bmParam.BMParamGetOne(param);
            //param.Id_Caja++;
            //param.Caja_Abierta = true;
            //param.Saldo_Caja = 0;
            //bmParam.BMParamUpdate(param);

            //caja = new Cajas();
            //caja.Id_Caja = param.Id_Caja;
            //// Crear registro de apertura de caja
            //caja.Tipo = 1;
            //caja.Fecha = DateTime.Now.Date;
            ////bmCajas.BMCajasInsert(caja);

            //// Crear registro de cierre de caja
            //caja.Tipo = 2;
            ////bmCajas.BMCajasInsert(caja);

            lblAccion.Text = "Caja Cerrada";
            btnCerrar.Visible = false;
            btnGuardar.Visible = false;
            btnCancelar.Text = "Salir";
            txtObs.ReadOnly = true;

            //param = null;
            //bmParam = null;
            caja = null;
            bmCajas = null;

        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region ActualizaGrilla
    public void ActualizaGrilla()
    {
        try
        {
            List<Cajas> listaCaja = new List<Cajas>();
            listaCaja = (List<Cajas>)Session["listaCajas"];
            gvCajas.DataSource = listaCaja;
            gvCajas.DataBind();

        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region PasarCaja
    public void PasarCaja()
    {
        try
        {
            Cajas cajaOrigen = new Cajas();
            cajaOrigen = (Cajas)Session["Caja"];
            Cajas cajaDestino = new Cajas();
            cajaDestino = (Cajas)Session["CajaDestino"];
            cajaDestino.Billete10 = cajaDestino.Billete10 + cajaOrigen.Billete10;
            cajaDestino.Billete100 = cajaDestino.Billete100 + cajaOrigen.Billete100;
            cajaDestino.Billete2 = cajaDestino.Billete2 + cajaOrigen.Billete2;
            cajaDestino.Billete20 = cajaDestino.Billete20 + cajaOrigen.Billete20;
            cajaDestino.Billete5 = cajaDestino.Billete5 + cajaOrigen.Billete5;
            cajaDestino.Billete50 = cajaDestino.Billete50 + cajaOrigen.Billete50;
            cajaDestino.Cheques = cajaDestino.Cheques + cajaOrigen.Cheques;
            cajaDestino.Ctv10 = cajaDestino.Ctv10 + cajaOrigen.Ctv10;
            cajaDestino.Ctv100 = cajaDestino.Ctv100 + cajaOrigen.Ctv100;
            cajaDestino.Ctv25 = cajaDestino.Ctv25 + cajaOrigen.Ctv25;
            cajaDestino.Ctv5 = cajaDestino.Ctv5 + cajaOrigen.Ctv5;
            cajaDestino.Ctv50 = cajaDestino.Ctv50 + cajaOrigen.Ctv50;
            cajaDestino.Total = cajaDestino.Total + cajaOrigen.Total;

            BMCajas bmCajas = new BMCajas(Connection);
            bmCajas.BMCajasUpdate(cajaDestino);

            PaseCaja pase = new PaseCaja();
            BMPaseCaja bmPase = new BMPaseCaja(Connection);

            pase.CajaOrigen = cajaOrigen.Id_Caja;
            pase.CajaDestino = cajaDestino.Id_Caja;
            pase.Fecha = DateTime.Now.Date;
            pase.Total = cajaOrigen.Total;
            pase = bmPase.BMPaseCajaInsert(pase);
            
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region DeshidrataC
    protected Cajas DeshidrataC()
    {
        Cajas caja = new Cajas();
        try
        {
            // Deshidrata Caja Cierre
            
            if (txtCtv5.Text != "0")
            {
                caja.Ctv5 = Convert.ToInt16(txtCtv5.Text.Trim());
            }
            if (txtCtv10.Text != "0")
            {
                caja.Ctv10 = Convert.ToInt16(txtCtv10.Text.Trim());
            }
            if (txtCtv25.Text != "0")
            {
                caja.Ctv25 = Convert.ToInt16(txtCtv25.Text.Trim());
            }
            if (txtCtv50.Text != "0")
            {
                caja.Ctv50 = Convert.ToInt16(txtCtv50.Text.Trim());
            }
            if (txtCtv100.Text != "0")
            {
                caja.Ctv100 = Convert.ToInt16(txtCtv100.Text.Trim());
            }
            if (txt2.Text != "0")
            {
                caja.Billete2 = Convert.ToInt16(txt2.Text.Trim());
            }
            if (txt5.Text != "0")
            {
                caja.Billete5 = Convert.ToInt16(txt5.Text.Trim());
            }
            if (txt10.Text != "0")
            {
                caja.Billete10 = Convert.ToInt16(txt10.Text.Trim());
            }
            if (txt20.Text != "0")
            {
                caja.Billete20 = Convert.ToInt16(txt20.Text.Trim());
            }
            if (txt50.Text != "0")
            {
                caja.Billete50 = Convert.ToInt16(txt50.Text.Trim());
            }
            if (txt100.Text != "0")
            {
                caja.Billete100 = Convert.ToInt16(txt100.Text.Trim());
            }
            if (txtTCheques.Text != "0")
            {
                caja.Cheques = Convert.ToDecimal(txtTCheques.Text.Trim());
            }
            caja.Efectivo = Convert.ToDecimal(txtTEfectivo.Text.Trim());
            caja.Diferencia = Convert.ToDecimal(txtTDifer.Text.Trim());

            caja.Fecha = DateTime.Now.Date;
            caja.Id_Caja = Convert.ToInt16(lblCaja.Text);
            caja.Tipo = 2; 

            caja.Id_Usuario = UsuarioActual().Id_Usuario;

            caja.Total = Convert.ToDecimal(txtTGeneral.Text.Trim());
            caja.Obs = txtObs.Text;

            
        }
        catch (Exception ex) { ErrorUI(ex); }
        return caja;
    }
    #endregion

    #region DeshidrataA
    protected Cajas DeshidrataA()
    {
        Cajas caja = new Cajas();
        try
        {
            // Deshidrata Caja Apertura

            if (txtACtv5.Text != "0")
            {
                caja.Ctv5 = Convert.ToInt16(txtACtv5.Text.Trim());
            }
            if (txtACtv10.Text != "0")
            {
                caja.Ctv10 = Convert.ToInt16(txtACtv10.Text.Trim());
            }
            if (txtACtv25.Text != "0")
            {
                caja.Ctv25 = Convert.ToInt16(txtACtv25.Text.Trim());
            }
            if (txtACtv50.Text != "0")
            {
                caja.Ctv50 = Convert.ToInt16(txtACtv50.Text.Trim());
            }
            if (txtACtv100.Text != "0")
            {
                caja.Ctv100 = Convert.ToInt16(txtACtv100.Text.Trim());
            }
            if (txtA2.Text != "0")
            {
                caja.Billete2 = Convert.ToInt16(txtA2.Text.Trim());
            }
            if (txtA5.Text != "0")
            {
                caja.Billete5 = Convert.ToInt16(txtA5.Text.Trim());
            }
            if (txtA10.Text != "0")
            {
                caja.Billete10 = Convert.ToInt16(txtA10.Text.Trim());
            }
            if (txtA20.Text != "0")
            {
                caja.Billete20 = Convert.ToInt16(txtA20.Text.Trim());
            }
            if (txtA50.Text != "0")
            {
                caja.Billete50 = Convert.ToInt16(txtA50.Text.Trim());
            }
            if (txtA100.Text != "0")
            {
                caja.Billete100 = Convert.ToInt16(txtA100.Text.Trim());
            }
            if (txtATCheques.Text != "0")
            {
                caja.Cheques = Convert.ToDecimal(txtATCheques.Text.Trim());
            }
            caja.Efectivo = Convert.ToDecimal(txtATEfectivo.Text.Trim());
            caja.Diferencia = 0;

            caja.Fecha = DateTime.Now.Date;
            caja.Id_Caja = Convert.ToInt16(lblCaja.Text);
            caja.Tipo = 2;

            Usuarios usuario = new Usuarios();
            usuario = (Usuarios)Session["UsuarioActual"];
            caja.Id_Usuario = usuario.Id_Usuario;

            caja.Total = caja.Efectivo + caja.Cheques;
            caja.Obs = txtObs.Text;

            
        }
        catch (Exception ex) { ErrorUI(ex); }
        return caja;
    }
    #endregion

    #endregion
    
}

